﻿#!/usr/bin/env python
# -*- coding: utf-8 -*-

'pickling and unpickling uages'

'''
把变量从内存中变成可存储或传输的过程称之为序列化，在Python中叫pickling，
在其他语言中也被称之为serialization，marshalling，flattening等等，都是一个意思。
'''

__author__='zhaoxp'

try:
	import cPickle as pickle
except ImportError:
	import pickle
	
def pickling(filepath,object):
	f=open(filepath,'wb')
	pickle.dump(object,f)
	f.close()
	
def unpickling(filepath):
	f=open(filepath,'rb')
	d=pickle.load(f)
	f.close()
	return d

if __name__=='__main__':
	print 'pickling and unpickling usage'
	d={'name':'Bob','age':20,'score':88}
	print "d =",d
	print 'pickling into file'
	pickling('d.txt',d)
	
	d2=unpickling('d.txt')
	print "d2 =",d2